package com.audaque.springboot.foshanupload.web.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


//方式1
//@Configuration
/*@EnableWebMvc
public class WebMvcConfig extends WebMvcAutoConfiguration implements WebMvcConfigurer {*/
//方式2
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {



    @Value("${http.handleTimeout}")
    private long handleTimeout;

    //跨域:方式1
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 所有接口
                .allowedOrigins("*")
                .allowCredentials(true) // 是否发送 Cookie
                .allowedOrigins("*") // 支持域
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS","PATCH") //支持请求方式
                .allowedHeaders("*") //允许的原始请求头部信息
                .exposedHeaders("*") //暴露的头部信息
                .maxAge(handleTimeout);
    }

    //解决  No mapping for GET /favicon.ico 访问静态资源图标
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/META-INF/resources/");
    }

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        /**
         * 缀模糊匹配，默认为true
         * true: 请求/index.do,/index.abc，index.xxx都能请求到
         * false: /index。后缀也需要匹配
         */

        configurer.setUseSuffixPatternMatch(true);
        /**
         * 尾斜杠匹配
         * true：/index和/index/都可以
         * false: 只能通过/index访问
         */
        configurer.setUseTrailingSlashMatch(true);
    }




}
